//
//  OrderDetailViewController.m
//  YYPHSpeedJump
//
//  Created by speedjump on 15/12/30.
//  Copyright © 2015年 sujiang. All rights reserved.
//

#import "OrderDetailViewController.h"
#import "SubmitOrderViewController.h"

@interface OrderDetailViewController ()
{
    NSTimer *timer;
}
@property (nonatomic, assign) NSInteger time;

@end

@implementation OrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = kColors(239, 239, 241, 1);
    self.title = @"订单详情";
    [self addSubViews];
}

- (void)addSubViews
{
    if ([self.model.status integerValue] == 1) {
        self.statusLb.text = @"等待付款";
        _time = self.model.remain_seconds.integerValue;
        NSInvocationOperation *opera = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(actionTimer) object:nil];
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        [queue addOperation:opera];

    } else if (self.model.status.integerValue == 7 || self.model.status.integerValue == 11){
        [self.payBtn removeFromSuperview];
        self.statusLb.text = @"支付超时!";
        self.remain_time = @"0";
    } else if (self.model.status.integerValue == 2){
        [self.payBtn removeFromSuperview];
        self.statusLb.text = @"已被别人购买!";
        self.remain_time = @"0";
    } else if (self.model.status.integerValue == 12) {
        [self.payBtn removeFromSuperview];
        self.statusLb.text = @"订单已退款!";
        self.remain_time = @"0";
    } else {
        [self.payBtn removeFromSuperview];
        self.statusLb.text = @"交易成功!";
        self.remain_time = @"0";
    }
    self.goodPic.contentMode = UIViewContentModeScaleAspectFit;
    [self.goodPic sd_setImageWithURL:[NSURL URLWithString:self.model.head_img_url] placeholderImage:[UIImage imageNamed:@"tu-xiao"]];
    self.nameLb.text = self.model.name;
    self.priceLb.text = [NSString stringWithFormat:@"￥%@", self.model.buy_price];
    self.realName.text = [NSString stringWithFormat:@"收货人: %@", self.model.real_name];
    self.phoneNum.text = [NSString stringWithFormat:@"联系电话: %@", self.model.phone];
    self.address.text = self.model.address;
    self.remainTime.text = [NSString stringWithFormat:@"%@", self.remain_time];
    self.order.text = [NSString stringWithFormat:@"订单号:%@", self.model.deal_id];
    self.creatTime.text = [NSString stringWithFormat:@"参与时间:%@", self.model.join_time];
    self.joinTime.text = [NSString stringWithFormat:@"成交时间:%@", self.model.deal_time];
}

- (IBAction)actionPay:(id)sender {
    
    SubmitOrderViewController *subVC = [[SubmitOrderViewController alloc] initWithNibName:@"SubmitOrderViewController" bundle:nil];
    subVC.pushType = @"1";
    [self.navigationController pushViewController:subVC animated:YES];
    subVC.sendModel = self.model;
    
}

- (void)actionTimer
{
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(changeTime) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] run];
    
    
}


- (void)changeTime
{
    if (self.time <= 0) {
        [timer invalidate];
    }
    self.time -= 1;
}

- (void)setTime:(NSInteger)time
{
    _time = time;
    [self performSelectorOnMainThread:@selector(actionRunTime) withObject:nil waitUntilDone:YES];
}

- (void)actionRunTime
{
    self.remainTime.text = [NSString stringWithFormat:@"%lds", _time];
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
